package com.xxx.yuedong.campaign.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONUtil;
import com.xxx.yuedong.campaign.controller.form.InsertScheduleForm;
import com.xxx.yuedong.campaign.controller.form.SearchAllForm;
import com.xxx.yuedong.campaign.pojo.Schedule;
import com.xxx.yuedong.campaign.service.ScheduleService;
import com.xxx.yuedong.campaign.service.SearchService;
import com.xxx.yuedong.common.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hc
 * @date 2023/09/04 15:30
 **/

@RestController
@RequestMapping("/search")
@Api(tags = "搜索相关接口")
@Slf4j
public class SearchController {
    @Autowired
    private SearchService searchService;

    @PostMapping("/searchAll")
    @ApiOperation(value = "搜索")
    public R insert(@Valid @RequestBody SearchAllForm form){
        log.debug("正在执行 【搜索】,参数为:{}" + form.toString());
        ArrayList<HashMap> list = searchService.searchAll(form.getKeyword());
        Map<String, List<HashMap>> ListMap = groupResultsByType(list);
        return R.ok().put("ListMap",ListMap);
    }

    public Map<String, List<HashMap>> groupResultsByType(ArrayList<HashMap> list) {
        Map<String, List<HashMap>> groupedResults = new HashMap<>();

        for (HashMap result : list) {
            String type = (String) result.get("type");

            if (!groupedResults.containsKey(type)) {
                groupedResults.put(type, new ArrayList<>());
            }

            groupedResults.get(type).add(result);
        }

        return groupedResults;
    }
}
